package com.doge.walknovel.business.strategy

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.adapters.toLiveData

class StrategyNovelListViewModel constructor(
    private val repository: StrategyNovelListRepository
) : ViewModel() {
    val loading: LiveData<LoadStatus> = repository.loading
    val dataList = repository.dataSource().toLiveData(
        cellId = { it.id },
        requestMore = { }) // 就查询10条.这个会无限循环查下一页 repository.request(false)

    fun request() {
        repository.request(true)
    }

}


class StrategyNovelListViewModelFactory(
    private val titleId: Int,
    private val tabId: Int?
) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val repository = StrategyNovelListRepository(titleId, tabId)
        if (modelClass.isAssignableFrom(StrategyNovelListViewModel::class.java)) {
            return StrategyNovelListViewModel(repository) as T
        }
        //noinspection unchecked
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

